import 'package:flutter/material.dart';
import 'package:flutter_app/ui/apply/duty_cancel_dialog/duty_cancel_dialog_view.dart';
import 'package:flutter_app/ui/apply/duty_dialog/duty_dialog_view.dart';
import 'package:flutter_app/utils/DialogUtils.dart';
import 'package:flutter_app/utils/toast_utils.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/CustomDialog.dart';
import 'package:flutter_app/widget/custom_app_bar.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_screenutil/src/size_extension.dart';
import 'package:get/get.dart';

import 'apply_detail_logic.dart';

/**
 * @Description: 报名详情
 * @Author: 下雨了
 * @CreateDate: 2021-10-18 14:08
 * @UpdateUser: 更新者
 * @UpdateDate:  2021-10-18 14:08
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
///
class ApplyDetailPage extends StatefulWidget {
  final String title;

  const ApplyDetailPage({Key? key, required this.title}) : super(key: key);

  @override
  _ApplyDetailPageState createState() => _ApplyDetailPageState();
}

class _ApplyDetailPageState extends State<ApplyDetailPage> {
  final logic = Get.put(ApplyDetailLogic());
  final state = Get.find<ApplyDetailLogic>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ApplyDetailLogic>(builder: (logic) {
      return Scaffold(
        appBar: MyAppBar(context, title: '报名详情'),
        body: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                margin: EdgeInsets.only(left: 16.r, right: 16.r, top: 35.r),
                child: Center(
                  child: Text(state.taskName,
                    textAlign: TextAlign.center,
                    style: TextStyle(color: Colors.black, fontSize: 17.sp, fontWeight: FontWeight.w600),
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.only(left: 16.r, right: 16.r, top: 25.r),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('单位名称：${state.orgName}',
                      style: TextStyle(color: MyColors.FF999999, fontSize: 11.sp),
                    ),
                    Text('发布时间：${state.createTime}',
                      style: TextStyle(color: MyColors.FF999999, fontSize: 11.sp),
                    )
                  ],
                ),
              ),
              SizedBox(height: 10.r,),
              Divider(indent: 16.r, endIndent: 16.r, color: MyColors.FFDDDDDD, height: 0.r,),
              Container(
                margin: EdgeInsets.only(left: 10.r, right: 10.r),
                child: Html(data: state.collectDetailEntity?.content ?? "",
                  style: {
                    "p": Style(
                        lineHeight: LineHeight(2),
                        fontSize: FontSize(17.sp),
                        fontWeight: FontWeight.w500,
                        direction: TextDirection.ltr,
                        textAlign: TextAlign.justify,
                        letterSpacing: 0.3
                    ),
                    "img": Style(
                      alignment: Alignment.center,
                    ),
                  },
                ),
              ),
            ],
          ),
        ),
        bottomNavigationBar: IntrinsicHeight(
          child: Container(
            padding: EdgeInsets.fromLTRB(31.r, 9.r, 31.r, 9.r),
            decoration: BoxDecoration(
                boxShadow: [BoxShadow(
                  color: Color(0x1a000000),
                  blurRadius: 6.67.r,
                  spreadRadius: 0.05.r,
                )],
                shape: BoxShape.rectangle,
                color: Colors.white
            ),
            child: SafeArea(
              child: SizedBox(
                height: 40.r,
                child: Row(
                  children: [
                    Expanded(
                      child: Center(
                        child: Text(
                          (state.collectDetailEntity?.totalSign ?? "0" )+ '人已报名',
                          style: TextStyle(color: MyColors.FF999999, fontSize: 14.sp),
                        ),
                      ),
                    ),
                    Expanded(
                      child: TextButton(
                        child: state.collectDetailEntity?.isLimit == '1'
                            ? Text('已过期', style: TextStyle(color: Colors.white, fontSize: 14.sp, fontWeight: FontWeight.w600),)
                            : state.collectDetailEntity?.status == '1'
                            ? Text('已报名', style: TextStyle(color: Colors.white, fontSize: 14.sp, fontWeight: FontWeight.w600),)
                            : Text('立即报名', style: TextStyle(color: Colors.white, fontSize: 14.sp, fontWeight: FontWeight.w600),),
                        style: TextButton.styleFrom(
                          backgroundColor: logic.getButtonColors(),
                          padding: EdgeInsets.all(0),
                          maximumSize: Size(160.r, 40.r)
                        ),
                        onPressed: () {
                          // isLimit 是否过期 0否 1是
                          if (state.collectDetailEntity?.isLimit == "1" ) {
                            toast.show("已过期");
                            return;
                          }
                          if (logic.taskType == "") {
                            toast.show("taskType为空, 联系管理员");
                            return;
                          }
                          if (logic.taskType == "1" || logic.taskType == "3") {
                            if (state.collectDetailEntity?.collectStatus == "3") {
                              toast.show("已结束");
                              return;
                            }
                            // status 报名状态 0未报名 1已报名
                            if (state.collectDetailEntity?.status == "1") {
                              toast.show("已报名");
                              return;
                            }
                            dialogUtils.show(
                              context: context,
                              title: "报名确认",
                              content: "您正在报名参加活动，报名后不可随意取消！",
                              callbackOk: () {
                                logic.postGoCollectSign().then((value) {
                                  if (value) {
                                    if (state.collectDetailEntity?.special == "") {
                                      logic.postCollectDetail();
                                      Navigator.of(this.context).pop("refresh");
                                      return;
                                    }
                                    showDialog(context: this.context, barrierDismissible: false, builder: (context){
                                      return CustomDialog(
                                        content: state.collectDetailEntity?.special ?? "",
                                        onPressed: () {
                                          logic.postCollectDetail();
                                          Navigator.of(this.context).pop("refresh");
                                        },
                                      );
                                    });
                                  }
                                });
                              },
                            );
                          } else {
                            showModalBottomSheet(
                              context: context,
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.only(topLeft: Radius.circular(13.33.r), topRight: Radius.circular(13.33.r))
                              ),
                              // isScrollControlled属性，设置为true，弹窗会覆盖整个手机。设置为false，弹窗大概是整个屏幕的9/16高。
                              isScrollControlled: true,
                              builder: (BuildContext context) {
                                if (state.collectDetailEntity?.collectStatus == "3") {
                                  return DutyCancelDialogPage(
                                    title: "管理值班日期",
                                    collectId: logic.collectId,
                                  );
                                } else {
                                  return DutyDialogPage(
                                    title: "值班弹窗",
                                    collectId: logic.collectId,
                                    status: state.collectDetailEntity!.status!,
                                    onRefreshCallback: (String collectId) {
                                      logic.postCollectDetail();
                                      logic.updateDynamicEvent();
                                    },
                                  );
                                }
                              },
                            );
                          }
                        },
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      );
    });
  }

  @override
  void dispose() {
    Get.delete<ApplyDetailLogic>();
    super.dispose();
  }
}
